package com.shop.servlet.impl;

import com.shop.dao.GoodsDao;
import com.shop.dao.ShopDao;
import com.shop.dao.TOrderDao;
import com.shop.dao.impl.GoodsDaoImpl;
import com.shop.dao.impl.OrderDaoImpl;
import com.shop.dao.impl.ShopCatDaoImpl;
import com.shop.dao.impl.TOrderDaoImpl;
import com.shop.entity.Order;
import com.shop.entity.ShopCat;
import com.shop.entity.ShowPageT;
import com.shop.entity.TOrder;
import com.shop.servlet.OrderServlet;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * com.shop.servlet.impl文档说明
 *
 * @author Angluzha
 * @version 2022/9/17
 */
public class OrderServletImpl extends BaseServletImpl<Order> implements OrderServlet {
    {
        dao = new OrderDaoImpl();
    }

    @Override
    public boolean createList(List<Order> orders) {
        OrderDaoImpl dao = (OrderDaoImpl) this.dao;
        ShopCatDaoImpl catDao = new ShopCatDaoImpl();
        try {
            orders.forEach(e->{
                dao.insert(e);
                catDao.deleteById(e.getGid());
            });
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public void removeTOrderById(Integer id) {
        ((TOrderDaoImpl)dao).deleteById(id);
    }

    @Override
    public boolean createOrder(Order order, List<Order> orders) {
        try {
            Integer sid = ((OrderDaoImpl)dao).insertGetKey(order);
            ShopDao shopCatDao = new ShopCatDaoImpl();
            TOrderDao tOrderDao = new TOrderDaoImpl();
            GoodsDao goodsDao = new GoodsDaoImpl();
            orders.forEach(e->{
                TOrder tOrder = new TOrder();
                tOrder.setUid(order.getUser());
                tOrder.setSid(sid);
                tOrder.setSpeed(e.getSpend());
                tOrder.setQuantity(e.getQuantity());
                tOrder.setGname(e.getGname());
                tOrder.setPnguri(e.getPnguri());
                tOrder.setDes(e.getDes());
                goodsDao.payFor(e.getGoods(),e.getQuantity());
                tOrderDao.insert(tOrder);
                shopCatDao.deleteById(e.getId());
            });
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public ShowPageT<TOrder> checkTorder(ShowPageT<TOrder> pageT) {
        TOrderDao tOrderDao = new TOrderDaoImpl();
        try {
            return tOrderDao.selectPage(pageT);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
